## Create Figure A2

## Loading packages
library(readxl)
library(tidyverse)
library(lubridate)
library(sf)
library(foreign)
library(haven)

## Load data
dta = read_stata("dta.dta")


## Share of spending in each category
tech_spend = sum(dta$bmp_tech, na.rm = T)/sum(dta$bmp_amount)*100 ## 30.37164
tech_spend
security_spend = sum(dta$bmp_security, na.rm = T)/sum(dta$bmp_amount)*100 ## 15.81797
security_spend
interior_spend = sum(dta$bmp_interior, na.rm = T)/sum(dta$bmp_amount)*100 ## 18.95186
interior_spend
exterior_spend = sum(dta$bmp_exterior, na.rm = T)/sum(dta$bmp_amount)*100 ## 34.61513
exterior_spend
other_spend = sum(dta$bmp_other, na.rm = T)/sum(dta$bmp_amount)*100 ## 0.3447397
other_spend

## Share of projects in each category
tech_proj = sum(dta$bmp_tech_n, na.rm = T)/sum(dta$bmp_n, na.rm = T)*100 ## 33.49398
tech_proj
security_proj = sum(dta$bmp_security_n, na.rm = T)/sum(dta$bmp_n, na.rm = T)*100 ## 28.43373
security_proj
interior_proj = sum(dta$bmp_interior_n, na.rm = T)/sum(dta$bmp_n, na.rm = T)*100 ## 17.3494
interior_proj
exterior_proj = sum(dta$bmp_exterior_n, na.rm = T)/sum(dta$bmp_n, na.rm = T)*100 ## 20.48193
exterior_proj
other_proj = sum(dta$bmp_other_n, na.rm = T)/sum(dta$bmp_n, na.rm = T)*100 ## 0.4819277
other_proj

## Plot
shares = c(tech_spend, security_spend, interior_spend,
           exterior_spend, other_spend, tech_proj, security_proj,
           interior_proj, exterior_proj, other_proj)
cat = c("Technology", "Security", "Interior", "Exterior", "Other",
        "Technology", "Security", "Interior", "Exterior", "Other")
type = c("Funding", "Funding", "Funding", "Funding", "Funding",
         "Projects", "Projects", "Projects", "Projects", "Projects")
bmp_shares = data.frame(shares, cat, type)

bmp_cat_shares = ggplot(bmp_shares, aes(x = factor(cat, level = c("Technology", "Security", "Exterior", "Interior", "Other")), y = shares,
                                        fill = factor(type))) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(label=
                  paste0(I(round(shares, digits = 2)), "%")), 
            position=position_dodge(width=0.9), vjust=-0.45, size = 2) +
  scale_fill_manual(name = "Share of", values = c("gray75", "gray45"),
                    labels = c("Funding", "Projects")) +
  labs(x = "Project Type", y = "Share") +
  scale_y_continuous(limits = c(0, 40), breaks = c(0, 10, 20, 30, 40),
                     labels = function(x) paste0(x, "%")) +
  theme_bw() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = "black"),
        legend.position = "bottom")